/**
 * 文件名称 : OpenglRender.java
 * <p>
 * 作者信息 : evanhu
 * <p>
 * 创建时间 : 2011-5-17, 下午02:15:09
 * <p>
 * 版权声明 : Copyright (c) 2009-2012 CIeNET Ltd. All rights reserved
 * <p>
 * 评审记录 :
 * <p>
 */

package com.ttomato.openglstart;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;

/**
 * 请在这里增加文件描述
 * <p>
 */
public class OpenglRender implements Renderer
{
    
    private static final String TAG = "OpenglRender";
    
    @Override
    public void onDrawFrame(GL10 gl)
    {
        // TODO Auto-generated method stub
        
        Log.e(TAG, "onDrawFrame");
        
        // Clears the screen and depth buffer.
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    }
    
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height)
    {
        // TODO Auto-generated method stub
        
        // Sets the current view port to the new size.
        gl.glViewport(0, 0, width, height);
        
        // Select the projection matrix
        gl.glMatrixMode(GL10.GL_PROJECTION);
        
        // Reset the projection matrix
        gl.glLoadIdentity();
        
        // Calculate the aspect ratio of the window
        GLU.gluPerspective(gl,
                45.0f,
                (float) width / (float) height,
                0.1f,
                100.0f);
        
        // Select the modelview matrix
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        
        // Reset the projection matrix
        gl.glLoadIdentity();
    }
    
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config)
    {
        // TODO Auto-generated method stub
        // set background black
        gl.glClearColor(0f, 0f, 0f, 0f);
        
        // Enable Smooth Shading, default not really needed.
        gl.glShadeModel(GL10.GL_SMOOTH);
        
        // Depth buffer setup.
        gl.glClearDepthf(1.0f);
        
        // Enables depth testing.
        gl.glEnable(GL10.GL_DEPTH_TEST);
        
        // The type of depth testing to do.
        gl.glDepthFunc(GL10.GL_LEQUAL);
        
        // Really nice perspective calculations.
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    }
    
}
